package config

import (
	flag "github.com/spf13/pflag"
	"log"
	"os"
)

type Config struct {
	NodeName string `mapstructure:"node-name"`
	// LogLevel is the log verbosity level used.
	// It can be (debug|info|warn|error|fatal|panic).
	LogLevel         string `mapstructure:"log-level"`
	WebHookPort      int    `mapstructure:"webhook-port"`
	WebHookServer    bool   `mapstructure:"webhook-enable"`
	WebHookAllowDeny bool   `mapstructure:"webhook-allowDeny"`
	WebHookCert      string `mapstructure:"webhook-crt"`
	WebHookKey       string `mapstructure:"webhook-key"`
	WebHookCa        string `mapstructure:"webhook-ca"`
	Controller       bool   `mapstructure:"controller-enable"`
	MQType           string `mapstructure:"mq-type"`
	MQURI            string `mapstructure:"mq-uri"`
	MQTopic          string `mapstructure:"mq-topic"`
}

var defaultWebHookPort int = 443

func DefaultConfig() *Config {
	hostname, err := os.Hostname()
	if err != nil {
		log.Panic(err)
	}
	return &Config{
		NodeName:         hostname,
		LogLevel:         "info",
		WebHookPort:      defaultWebHookPort,
		WebHookServer:    true,
		WebHookAllowDeny: false,
		Controller:       true,
	}
}

// ConfigFlagSet creates all of our configuration flags.
func ConfigFlagSet() *flag.FlagSet {
	c := DefaultConfig()
	cmdFlags := flag.NewFlagSet("orderhook flagset", flag.ContinueOnError)
	cmdFlags.String("node-name", c.NodeName,
		"Name of this node.")
	return cmdFlags
}
